<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * Author: 当燃      
 * Date: 2015-09-21
 */

namespace Admin\Controller;

class MessagesController extends BaseController{

    public function message_list(){
        $content=I('content');
        if($content){
            $where['content']=array('like','%'.$content.'%');
            $this->assign('content',$content);
        }
        $model=M('messages');
        $count = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $lists = $model->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('lists',$lists);
        $this->display();
    }

    public function detail(){
        $id=I('id');
        $where['id']=$id;
        $model=M('messages');
        $data = $model->where($where)->find();
        $re_lists=M('messages_re')->where(array('m_id'=>$id))->select();
        $user =M('users')->where(array('user_id'=>$data['user_id']))->find();
        $this->assign('lists',$re_lists);
        $this->assign('data',$data);
        $this->assign('user',$user);
        $this->display();
    }

    public function message_re(){
        $m_id=I('m_id');
        $re_content=I('re_content');
        $data['m_id']=$m_id;
        $data['content']=$re_content;
        $data['add_time']=time();
        $data['user_id']=0;
        $res=M('messages_re')->add($data);
        if(!$res){
            $this->error('回复失败，请重试！');
        }else{
            $data_m['id']=$m_id;
            $data_m['is_check']=1;
            $data_m['re_time']=time();
            M('messages')->save($data_m);//将该条问题变成是已回复
            //发送邮件通知
            $messages=M('messages')->where(array('id'=>$m_id))->find();
            $user=M('users')->where(array('user_id'=>$messages[user_id]))->find();
            $user['email']='459796567@qq.com';
            $content='您在娃娃网站提的问题（'.$messages[content].'）,管理员已回复（'.$re_content.'）！';//待翻译
            $a=send_email($user['email'],'Question reply',$content);
            $this->success('回复成功。');
        }

    }


    /**
     * 删除回复
     */
    public function delete_re(){
        $where['id']=I('id');
        $res=M('messages_re')->where($where)->delete();

        if(!$res)
            $this->error('删除失败，请重试！');
        else
            $this->success('删除成功。');
    }


    /**
     * 删除问答
     */
    public function delete_message(){
        $where['id']=I('id');
        $where_re['m_id']=I('id');
        M('messages_re')->where($where_re)->delete();//删除回复
        $res=M('messages')->where($where)->delete();//删除提问
        if(!$res)
            $this->error('删除失败，请重试！');
        else
            $this->success('删除成功。');
    }


    /************************************************ 用户留言管理 ******************************************************/


    public function leave_message_list(){
//        $status=I('status');
//        if(!empty($status))
//            $where['status']=$status-1;
        $model=M('leave_message');

        $count = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $lists = $model->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('lists',$lists);
//        $this->assign('status',$status);
        $this->display();

    }



    public function leave_detail(){
        $id=I('id');
        $where['id']=$id;
        $model=M('leave_message');
        $data = $model->where($where)->find();
        $up_data['id']=$id;
        $up_data['status']=1;
        $model->save($up_data);//更新为已查看
        $this->assign('data',$data);
        $this->display();
    }





    public function delete_leave_message(){
        $where['id']=I('id');
        $res=M('leave_message')->where($where)->delete();//删除提问
        if(!$res)
            $this->error('删除失败，请重试！');
        else
            $this->success('删除成功。');
    }


}